// components/common/bind-mobile-select/index.js
import componentBehavior from '../../../behaviors/component';
import { COMPONENT_OPTIONS } from '../../../constants/common';
import { getCode, showTabbar, hideTabbar, tipSuccess } from '../../../common/env';
import { bindMobileEncrypted } from '../../../api/usr';

Component({
    behaviors: [componentBehavior],
    options: {
        ...COMPONENT_OPTIONS,
    },
    properties: {
        show: {
            type: Boolean,
            value: false,
        },
        containsHeader: {
            type: Boolean,
            value: true,
        },
    },
    observers: {
        'show': function (val) {
            val ? hideTabbar() : showTabbar();
        },
    },
    data: {
        isAgreed: true,
        bindMobileDialog: {
            show: false,
        },
        agreementPopup: {
            show: false,
        },
    },
    methods: {
        handleClose(e) {
            this.setData({
                show: false,
            });
            // this.triggerEvent('close');
        },
        async handelGetMobile(e) {
            const { encryptedData, iv } = e.detail;
            if (encryptedData && iv) {
                const params = { encryptedData, iv };
                params.code = await getCode();
                console.log('GetMobile => 获取微信手机加密数据', JSON.stringify(params));
                const info = await bindMobileEncrypted(params);
                if (info) {
                    this.triggerEvent('result', {
                        mobile: info.mobile,
                    });
                    tipSuccess('绑定成功');
                    this.handleClose();
                }
            }
        },
        async handleGetBindMobile(e) {
            const info = e.detail;
            console.log('GetMobile => 获取绑定手机号', JSON.stringify(info));
            this.triggerEvent('result', {
                mobile: info.mobile,
            });
            this.handleClose();
        },
        handleToggleAgree() {
            const { isAgreed } = this.data;
            this.setData({
                isAgreed: !isAgreed,
            });
        },
        handleShowBindMobileDialog() {
            this.setData({
                'bindMobileDialog.show': true,
            });
        },
        handleShowAgreementPopup() {
            this.setData({
                'agreementPopup.show': true,
            });
        },
    }
})
